package com.heima.user.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.ApUserRealNameDto;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealName;
import com.heima.user.mapper.ApUserRealNameMapper;
import com.heima.user.service.ApUserRealNameService;
import com.heima.user.service.ApUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class ApUserRealNameServiceImpl extends ServiceImpl<ApUserRealNameMapper, ApUserRealName> implements ApUserRealNameService {

    @Autowired
    private ApUserService apUserService;

    @Override
    public ResponseResult loadListByStatus(AuthDto authDto) {
        //1.设置分页参数
        authDto.checkParam();

        //2.封装查询条件
        LambdaQueryWrapper<ApUserRealName> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(authDto.getStatus() !=null,ApUserRealName::getStatus,authDto.getStatus());

        Page page = new Page(authDto.getPage(), authDto.getSize());
        page(page,queryWrapper);

        List<ApUserRealName> records = page.getRecords();
        List<ApUserRealNameDto> realNameDtos = new ArrayList<>();
        ApUser apUser = null;
        ApUserRealNameDto apUserRealNameDto = null;
        for (ApUserRealName apUserRealName : records) {
            apUser = apUserService.getById(apUserRealName.getId());
            apUserRealNameDto = new ApUserRealNameDto();
            BeanUtils.copyProperties(apUserRealName,apUserRealNameDto);
            apUserRealNameDto.setName(apUser.getName());
            realNameDtos.add(apUserRealNameDto);
        }

        return ResponseResult.okResult(realNameDtos);

    }

    @Override
    public ResponseResult authFail(AuthDto authDto) {
        //参数检查
        if(authDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUserRealName apUserRealName = getOne(Wrappers.<ApUserRealName>lambdaQuery().eq(ApUserRealName::getId, authDto.getId()));
        if (apUserRealName == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        apUserRealName.setReason(authDto.getMsg());
        apUserRealName.setStatus(2);

        updateById(apUserRealName);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult autSuccess(AuthDto authDto) {
        //参数检查
        if(authDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUserRealName apUserRealName = getOne(Wrappers.<ApUserRealName>lambdaQuery().eq(ApUserRealName::getId, authDto.getId()));
        if (apUserRealName == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        apUserRealName.setStatus(9);

        updateById(apUserRealName);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
